﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Design;
using ProgNetComponentsDx.Language;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;

namespace ProgNetComponentsDx.Language.Design.Designers
{
    public class LanguageProviderDesigner : ProgNetComponentsDx.Design.DxGridColumnDesigner
    {
        public override DesignerVerbCollection Verbs
        {
            get
            {
                DesignerVerbCollection v = base.Verbs;
                v.Add(new DesignerVerb("Default Columns Names", new EventHandler(DefaultColumnsNames)));
                v.Add(new DesignerVerb("Create Labels for Edits", new EventHandler(CreateLabels)));
                return v;
            }
        }

        private void DefaultColumnsNames(object sender, EventArgs e)
        {
            LanguageProvider p = Component as LanguageProvider;
            if (p != null)
            {
                p.BeginInit();
                foreach (KeyValuePair<object, LanguageSettings> kvp in p.AllObjects)
                {
                    if (kvp.Key is GridColumn)
                    {
                        kvp.Value.SetPropertyValueWithPropertyDescriptor("StringCode", (kvp.Key as GridColumn).FieldName);
                    }
                }
                p.EndInit();
            }
        }

        private void CreateLabels(object sender, EventArgs e)
        {
            LanguageProvider p = Component as LanguageProvider;
            IDesignerHost host = this.GetService(typeof(IDesignerHost)) as IDesignerHost;
            if (host != null)
            {
                Control c = host.RootComponent as Control;
                if (c != null)
                {
                    CreateLables(host, c);
                }
            }
        }
        private int lblSize = 150;
        private int space = 5;

        private void CreateLables(IDesignerHost host, Control control)
        {
            LanguageProvider p = Component as LanguageProvider;
            foreach (Control c in control.Controls)
            {
                if (c is BaseEdit && c.DataBindings["EditValue"] != null)
                {
                    string member = c.DataBindings["EditValue"].BindingMemberInfo.BindingMember;
                    if (c is CheckEdit)
                    {
                        LanguageSettings s = p.GetSettings(c);
                        if (s != null)
                        {
                            s.SetPropertyValueWithPropertyDescriptor("StringCode", member);
                        }
                    }
                    else
                    {
                        string lblName = "lbl" + member;
                        if (control.Controls[lblName] == null)
                        {
                            try
                            {
                                int left = c.Left;
                                int lblLeft = left - lblSize - space;
                                if (lblLeft < 5)
                                    lblLeft = 5;
                                left = lblLeft + lblSize + space;
                                c.Left = left;

                                LabelControl lbl = host.CreateComponent(typeof(LabelControl), lblName) as LabelControl;
                                lbl.AutoSizeMode = LabelAutoSizeMode.None;
                                lbl.Left = lblLeft;
                                lbl.Top = c.Top;
                                lbl.Height = 20;
                                lbl.Width = lblSize;
                                lbl.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
                                control.Controls.Add(lbl);
                                LanguageSettings s = p.GetSettings(lbl);
                                if (s != null)
                                {
                                    s.StringCode = member;
                                    s.Surfix = ":";
                                }
                            }
                            catch { }
                        }
                    }
                }
                else
                {
                    CreateLables(host, c);
                }
            }
        }
    }
}
